<template>
  <div class="wrap">
    <div class="search-wrap">
      <el-input
        v-model.trim="search.title"
        @keyup.enter="getDrawList"
        placeholder="模糊搜索草稿"
        style="width: 220px"
        size="default"
      />
      <el-button
        class="search-class"
        size="default"
        type="primary"
        @click.stop="getDrawList"
        >查询</el-button
      >
    </div>
    <div class="table-header">
      <div>草稿</div>
      <el-button class="add" type="primary" size="default" @click="addDraw"
        >新增</el-button
      >
    </div>
    <el-table :data="drawList" style="width: 100%">
      <!-- <el-table-column prop="id" label="id"> </el-table-column> -->
      <el-table-column prop="title" label="草稿"> </el-table-column>
      <el-table-column prop="created_at" label="创建时间"> </el-table-column>
      <el-table-column prop="updated_at" label="更新时间"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template v-slot="scope">
          <el-button
            size="small"
            type="primary"
            @click.stop="editorRow(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            size="small"
            type="primary"
            @click.stop="previewRow(scope.row)"
          >
            预览
          </el-button>
          <el-popconfirm
            title="确认要删除该列?"
            confirm-button-text="是"
            cancel-button-text="否"
            @confirm="deleteRow(scope.row)"
          >
            <template #reference>
              <el-button> 删除 </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-footer">
      <el-pagination
        background
        @current-change="currentChange"
        :page-size="pagination.pageSize"
        layout="prev, pager, next"
        :total="pagination.total"
      />
    </div>
  </div>
</template>
  
  <script>
import request from "utils/request";
import { ElMessage, ElMessageBox } from "element-plus";

export default {
  name: "NestAdminDraw",

  data() {
    return {
      dialogDrawVisible: false,
      dialogDrawTripType: 1, //1 新增 0 编辑
      search: {
        title: "",
      },
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0,
      },
      form: {
        id: "",
        title: "",
      },
      drawList: [],
    };
  },

  async mounted() {
    await this.getDrawList();
  },
  watch: {
    dialogDrawVisible: {
      handler(value) {
        if (!value) {
          Object.assign(this.form, {
            id: "",
            title: "",
          });
        }
      },
    },
  },
  computed: {
    userId() {
      return this.$store.state.user.userInfo.id;
    },
  },
  methods: {
    async getDrawList() {
      let ret = await request.get("/draw/list", {
        title: this.search.title,
        userId: this.userId,
        currentPage: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
      });
      if (ret.code == 200) {
        this.drawList = ret.data[0] || [];
        this.pagination.total = ret.data[1];
      }
    },
    async confirmAddDraw() {
      let ret = {};
      if (this.dialogDrawTripType == 1) {
        ret = await request.post("/draw/add", {
          title: this.form.title,
        });
      } else {
        ret = await request.patch(`/draw/${this.form.id}`, {
          title: this.form.title,
        });
      }

      if (ret.code == 200) {
        this.$message({
          message: this.dialogDrawTripType == 1 ? "新增成功" : "编辑成功",
          type: "success",
        });
        this.getDrawList();
        this.dialogDrawVisible = false;
      } else {
        this.$message({
          message: ret.message,
          type: "error",
        });
      }
    },
    addDraw() {
      this.$router.push({
        path: "/draw-image",
        query: {},
      });
    },
    editorRow(row) {
      this.$router.push({
        path: "/draw-image",
        query: {
          id: row.id,
        },
      });
    },
    previewRow(row) {
      window.open(row.content, "__blank");
    },
    async deleteRow(row) {
      let ret = await request.delete(`/draw/${row.id}`, {});
      if (ret.code == 200) {
        ElMessage.success("删除成功");
        this.getDrawList();
      }
    },
    async currentChange(pageSize) {
      this.pagination.currentPage = pageSize;
      this.getDrawList();
    },
  },
};
</script>
  
  <style lang="scss" scoped>
.wrap {
  .headPictureCol {
    width: 32px;
    height: 32px;
    overflow: hidden;
    border-radius: 50%;
  }
  .search-wrap {
    display: flex;
    margin-bottom: 15px;
    .search-class {
      margin-left: 20px;
    }
  }
  .table-header {
    display: flex;
    line-height: 50px;
    padding: 0 10px;
    justify-content: space-between;
    align-items: center;
  }
  .pagination-footer {
    padding: 20px;
    display: flex;
    justify-content: flex-end;
  }
}


</style>

  